package com.wardrobe.soa.utils;

import com.google.common.collect.Lists;
import com.wardrobe.domain.booking.ApiBookingShowBean;
import com.wardrobe.domain.booking.BookingShowBean;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by m1304 on 2018/10/16.
 */
public class LocationUtils {
    private static double EARTH_RADIUS = 6378.137;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 通过经纬度获取距离(单位：米)
     *
     * @param lat1 纬度
     * @param lng1 经度
     * @param lat2
     * @param lng2
     * @return 距离
     */
    public static double getDistance(double lat1, double lng1, double lat2,
                                     double lng2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000d) / 10000d;
        s = s * 1000;
        return s;
    }

    public static String getDisStr(String pref, double distance){
        return pref + new BigDecimal(distance).divide(new BigDecimal(1000),2,BigDecimal.ROUND_HALF_UP).toString() + "km";
    }

    /**
     * 快速排序
     * java 集合中的sort 都是插入排序
     * @param temp
     * @param left
     * @param right
     * @param type
     * @param order
     */
    public static void quickSort(List<BookingShowBean> temp, int left, int right, int type , String order){
        if(left >= right){
            return;
        }

        int i = left;int j = right; BookingShowBean key = temp.get(left);

        while (i < j){

            if(order.equals("ASC")){
                while (i < j && (type == 1 ? temp.get(j).getDistance() >= key.getDistance() : temp.get(j).getWatch() >= key.getWatch())){
                    j--;
                }
            }else{
                while (i < j && (type == 1 ? temp.get(j).getDistance() <= key.getDistance() : temp.get(j).getWatch() <= key.getWatch())){
                    j--;
                }
            }


            if(i < j){
                temp.set(i,temp.get(j));
                i++;
            }

            if(order.equals("ASC")){
                while (i < j && (type == 1 ? temp.get(i).getDistance() < key.getDistance() : temp.get(i).getWatch() < key.getWatch())){
                    i++;
                }
            }else {
                while (i < j && (type == 1 ? temp.get(i).getDistance() > key.getDistance() : temp.get(i).getWatch() > key.getWatch())){
                    i++;
                }
            }

            if(i < j){
                temp.set(j,temp.get(i));
                j--;
            }

        }
        temp.set(i,key);
        quickSort(temp,left,i-1,type,order);
        quickSort(temp,i+1,right,type,order);
    }

    public static void main(String[] args) {
       /* double[] a = new double[]{6.22,2.22,1.222,4.52,3.22,102.23,123.333,44.44,2.1,-1.2,-12,44,44.44};
        List<ShopBean> res = Lists.newArrayList();
        Random random = new Random();
        for(int i=0;i<a.length;i++){
            ShopBean bean = new ShopBean();
            bean.setId(i);
            bean.setDistance(a[i]);
            int n = random.nextInt(1000);
            bean.setWatch(n);
            bean.setClassify(random.nextInt(10));
            res.add(bean);
        }

        ShopTestBean bean = new ShopTestBean();
        quickSort(res,0,a.length-1,1,"ASC");

        for(int i=0;i<a.length;i++){
            System.out.println(res.get(i).getDistance() + "-" + res.get(i).getWatch());
        }
        System.out.println("----------------");
        bean.setA(res);
        quickSort(res,0,a.length-1,2,"DESC");

        for(int i=0;i<a.length;i++){
            System.out.println(res.get(i).getDistance() + "-" + res.get(i).getWatch());
        }
        System.out.println("----------------");
        bean.setB(res);
        for(int i=0;i<a.length;i++){
            System.out.println(bean.getA().get(i).getDistance() + "-" + bean.getA().get(i).getWatch());
            System.out.println(bean.getB().get(i).getDistance() + "-" + bean.getB().get(i).getWatch());
        }*/

       List<Integer> list = Lists.newArrayList(1,2,3,4,5);
       if(list.size() > 4){
           List<Integer> list1 = list.subList(0, 4);
           List<Integer> list2 = list.subList(4, list.size());
           System.out.println(list1);
           System.out.println(list2);
       }else{
           System.out.println(list);
       }

    }

}
